package com.m3839.sdk.anti.model;

import com.m3839.sdk.common.GlobalManager;
import com.m3839.sdk.common.core.ApiError;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.env.EnvConfig;
import com.m3839.sdk.common.env2.ApiManager;
import com.m3839.sdk.common.event.SubjectBus;
import com.m3839.sdk.common.http.IHttpManager;
import com.m3839.sdk.common.http.listener.SimpleHttpRequestListener;
import com.m3839.sdk.common.util.LogUtils;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:HykbFcmModel
 *
 * @author 陈宝阳
 * @create 2019/12/31 10: 44
 */
public class AntiApiModel implements IAntiModel {

  private static final String ANTI_VALID_PATH = "hykb/heartbeat";

  /**
   * 这个新接口有待确定，防沉迷的测试环境到底是平台ID，还是地址控制
   */
  private static final String ANTI_VALID_PATH_TEST = "hykb/heartbeat_test";

  private static final String KEY_GAME_ID = "gid";
  private static final String KEY_USER_ID = "uid";
  private static final String KEY_TOKEN = "token";
  private static final String KEY_DURATION = "duration";

  @Override
  public void requestAntiData(String gameId, String userId, String token, long duration) {

    EnvConfig config = ApiManager.getInstance().getAntiApiEnvConfig();
    String url = ApiManager.getInstance().buildAntiUrl(ANTI_VALID_PATH_TEST);
    IHttpManager httpManager = GlobalManager.getInstance().getHttpManager();

    final Map<String,Object> jsonParam = new HashMap<>();
    jsonParam.put(KEY_GAME_ID, gameId);
    jsonParam.put(KEY_USER_ID, userId);
    jsonParam.put(KEY_TOKEN, token);
    jsonParam.put(KEY_DURATION, duration);
    ApiManager.getInstance().checkParam(config, jsonParam);

    httpManager.requestGet(url, jsonParam,
            new SimpleHttpRequestListener() {

                 @Override
                 public void onResult(String response) throws JSONException {
                     LogUtils.flog("requestAntiData", "response=" + response);
                     SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_LOAD_OK, response);
                 }

                @Override
                public void onError(ApiError apiError) {
                    SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_LOAD_FAILED, null);
                    LogUtils.flog("requestAntiData",apiError.toString());
                }
    });
  }
}
